import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:collins_vocabulary/components/word_detail.dart';
import 'package:collins_vocabulary/model/word.dart';
import 'package:collins_vocabulary/model/db.dart';
import 'dart:async';

class VocabularyList extends StatefulWidget {
  final SharedPreferences prefs;
  final String title;
  final String label;
  VocabularyList({Key key, this.prefs, this.title, this.label})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return VocabularyListState();
  }
}

class VocabularyListState extends State<VocabularyList> {
  List list;
  DBClient client;

  @override
  initState() {
    super.initState();
    client = DBClient();
  }

  @override
  dispose() {
    super.dispose();
  }

  Widget buildItem(BuildContext context, int index) {
    Word item = Word().getDetail(list[index]);
    return Container(
      key: Key(index.toString()),
      margin: const EdgeInsets.all(7),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(.1),
            offset: Offset.zero,
            blurRadius: 15,
            spreadRadius: 0.1,
          )
        ],
      ),
      child: InkWell(
        child: Container(
          padding: const EdgeInsets.all(10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(item.text, style: TextStyle(fontSize: 14)),
              Icon(Icons.arrow_forward_ios, size: 12)
            ],
          ),
        ),
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => WordDetail(
                item: item,
                prefs: widget.prefs,
              ),
            ),
          );
        },
      ),
    );
  }

  Future<List> getList() async {
    List wholeList = await Word().getList(widget.prefs.getInt('level'));
    if (widget.label == 'all') {
      return wholeList;
    }
    List studiedList = await client.queryAll();
    if (widget.label == 'studied') {
      List studied = wholeList.where((item) {
        return studiedList.contains(item['word']);
      }).toList();
      return studied;
    }
    if (widget.label == 'unstudy') {
      List unstudy = wholeList.where((item) {
        return !studiedList.contains(item['word']);
      }).toList();
      return unstudy;
    } else {
      List lastWords = wholeList.where((item) {
        return !studiedList.contains(item['word']);
      }).toList();
      int count = widget.prefs.getInt('count');
      int len = count > lastWords.length ? lastWords.length : count;
      return lastWords.sublist(0, len);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        title: Text(
          widget.title,
          style: TextStyle(
            color: Color(0xFF7F8AEE),
          ),
        ),
        iconTheme: IconThemeData(color: Color(0xFF7F8AEE)),
        backgroundColor: Colors.white,
      ),
      body: FutureBuilder(
        future: getList(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            list = snapshot.data;
            return ListView.builder(
              itemBuilder: buildItem,
              itemCount: list.length,
            );
          }
          return Container(child: null);
        },
      ),
    );
  }
}
